<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.scrollTop()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/css/">CSS</a></span><span class="category-divider"> | </span><span class="category"><a href="/category/offset/">偏移</a></span><span class="category-divider"> | </span><span class="category"><a href="/category/manipulation/">DOM 操作</a> &gt; <a href="/category/manipulation/style-properties/">CSS 属性</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/scrollTop/" target="_blank">.scrollTop()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<div class="toc">
<h4><span>Contents:</span></h4>
<ul class="toc-list">
<li>
<a href="#scrollTop1">.scrollTop()</a><ul><li>.scrollTop()</li></ul>
</li>
<li>
<a href="#scrollTop2">.scrollTop( value )</a><ul><li>.scrollTop( value )</li></ul>
</li>
</ul>
</div><article class="entry method" id="scrollTop1"><h2 class="section-title">
<span class="name">.scrollTop()</span><span class="returns">返回: <a href="/Types/#Integer">Integer</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>获取匹配的元素集合中第一个元素的当前垂直滚动条的位置或设置每个匹配元素的垂直滚动条位置。</p>
<ul class="signatures"><li class="signature" id="scrollTop">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.2.6/">1.2.6</a></span>.scrollTop()</h4>
<ul><li><div class="null-signature">这个方法不接受任何参数。</div></li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p>滚动条垂直位置和滚动区隐藏区域的顶部高度像素值是相同的。如果滚动条是在最顶部，或者这个元素没有可滚动的，那么这个数字是<code>0</code>。</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">获取段落的scrollTop。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">p</span> <span class="rules">{ <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">10</span>px;</span></span><span class="rule"><span class="attribute">padding</span>:<span class="value"><span class="number">5</span>px;</span></span><span class="rule"><span class="attribute">border</span>:<span class="value"><span class="number">2</span>px solid <span class="hexcolor">#666</span>;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">p</span>&gt;</span>Hello<span class="tag">&lt;/<span class="title">p</span>&gt;</span><span class="tag">&lt;<span class="title">p</span>&gt;</span><span class="tag">&lt;/<span class="title">p</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"><span class="keyword">var</span> p = $(<span class="string">"p:first"</span>);</span></code></div></div><div class="container"><div class="line"><code>$(<span class="string">"p:last"</span>).text( <span class="string">"scrollTop:"</span> + p.scrollTop() );</code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article><article class="entry method" id="scrollTop2"><h2 class="section-title">
<span class="name">.scrollTop( value )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>设置每个匹配元素的垂直滚动条位置</p>
<ul class="signatures"><li class="signature" id="scrollTop-value">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.2.6/">1.2.6</a></span>.scrollTop( value )</h4>
<ul><li>
<div><strong>value</strong></div>
<div>类型: <a href="/Types/#Number">Number</a>
</div>
<div>一个用来设置滚动条垂直位置的正整数。</div>
</li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc-1">
<p>滚动条垂直位置和滚动区隐藏区域的顶部高度像素值是相同的。<code>scrollTop</code>为每个匹配元素设置滚动条的垂直位置。</p>
</div>
<section class="entry-examples" id="entry-examples-1"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-1-0">
<h4><span class="desc">设置一个div的scrollTop。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code><span class="tag">div</span><span class="class">.demo</span> <span class="rules">{</span></code></div></div><div class="container"><div class="line"><code><span class="rule"><span class="attribute">background</span>:<span class="value"><span class="hexcolor">#CCCCCC</span> none repeat scroll <span class="number">0</span> <span class="number">0</span>;</span></span></code></div></div><div class="container"><div class="line"><code><span class="rule"><span class="attribute">border</span>:<span class="value"><span class="number">3</span>px solid <span class="hexcolor">#666666</span>;</span></span></code></div></div><div class="container"><div class="line"><code><span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">5</span>px;</span></span></code></div></div><div class="container"><div class="line"><code><span class="rule"><span class="attribute">padding</span>:<span class="value"><span class="number">5</span>px;</span></span></code></div></div><div class="container"><div class="line"><code><span class="rule"><span class="attribute">position</span>:<span class="value">relative;</span></span></code></div></div><div class="container"><div class="line"><code><span class="rule"><span class="attribute">width</span>:<span class="value"><span class="number">200</span>px;</span></span></code></div></div><div class="container"><div class="line"><code><span class="rule"><span class="attribute">height</span>:<span class="value"><span class="number">100</span>px;</span></span></code></div></div><div class="container"><div class="line"><code><span class="rule"><span class="attribute">overflow</span>:<span class="value">auto;</span></span></code></div></div><div class="container"><div class="line"><code><span class="rule">}</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">p</span> <span class="rules">{ <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">10</span>px;</span></span><span class="rule"><span class="attribute">padding</span>:<span class="value"><span class="number">5</span>px;</span></span><span class="rule"><span class="attribute">border</span>:<span class="value"><span class="number">2</span>px solid <span class="hexcolor">#666</span>;</span></span><span class="rule"><span class="attribute">width</span>:<span class="value"><span class="number">1000</span>px;</span></span><span class="rule"><span class="attribute">height</span>:<span class="value"><span class="number">1000</span>px;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"demo"</span>&gt;</span><span class="tag">&lt;<span class="title">h1</span>&gt;</span>lalala<span class="tag">&lt;/<span class="title">h1</span>&gt;</span><span class="tag">&lt;<span class="title">p</span>&gt;</span>Hello<span class="tag">&lt;/<span class="title">p</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript">$(<span class="string">"div.demo"</span>).scrollTop(<span class="number">300</span>);</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article> </div>
</article>